package com.fengxu.netty.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

// 客户端管道处理器
public class NettyClientHandler extends ChannelInboundHandlerAdapter {

    // 当通道就绪时触发的事件
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        StudentPOJO.Student student = StudentPOJO.Student.newBuilder()
                                                        .setId(1)
                                                        .setName("欧鸡鸡")
                                                        .build();
        ctx.writeAndFlush(student);
    }

    // 通道可读事件
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        System.out.println("收到服务器的消息:"+byteBuf.toString(CharsetUtil.UTF_8));
        System.out.println("服务器地址:"+ctx.channel().remoteAddress().toString().substring(1));
    }
}
